package com.scu.bank.core.aop;

import com.github.pagehelper.PageInfo;
import com.scu.bank.core.jo.Result;
import com.scu.bank.util.PageHelperFilterUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 对控制器返回结果进行拦截
 */
@Component
@Aspect
public class ControllerResultAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(ControllerResultAspect.class);
    //对包下所有的controller结尾的类的所有方法增强
    private final String executeExpr = "execution(* com.scu.bank..*Controller.*(..))";

    /**
     * 对PageInfo返回进行拦截
     * @param joinPoint
     * @param keys
     */
    @AfterReturning(value = executeExpr, returning = "keys")
    public void doAfterReturningAdvice1(JoinPoint joinPoint, Object keys) {
        if (keys instanceof Result) {
            Result result = (Result) keys;
            Object data = result.getData();
            if (data instanceof PageInfo){
                result.setData(PageHelperFilterUtils.convert((PageInfo) data));
            }
        }
    }
}
